import { Injectable } from '@nestjs/common';
import { Repository, getRepository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { UserMap } from './entity/friend.entity';

@Injectable()
export class FriendService {
  constructor(
    @InjectRepository(UserMap)
    private readonly friendRepository: Repository<UserMap>,
    // @InjectRepository(FriendMessage)
    // private readonly friendMessageRepository: Repository<FriendMessage>,
  ){ }

  /**获取好友列表 */
  async getFriends(uid) {
    try {
      if(uid) {
        return { success: true, value: await this.friendRepository.find(uid) };
      } else {
        return { success: false, value: await this.friendRepository.find() };
      }
    } catch(e) {
      return { success: false, value: e };
    }
  }

  async isFriend(uids: UserMap) {
    try {
      if(uids) {
        const row = await this.friendRepository.find({
          where: { uid: uids.uid, friendId: uids.friendId }
        });
        return row.length ? { success: true, isFriend: true } 
                          : { success: false, isFriend: false };
      }
    } catch(e) {
      return { success: false, value: e };
    }
  }
}